/********************************************************************************
 * Project Name		[JavaEE_JDBC]
 * File Name     	[TestC3P0.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package pool.c3p0;

import java.sql.Connection;

import javax.sql.DataSource;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 测试c3p0连接池
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestC3P0 {

	/**
	 * 基本用法
	 * 
	 * @throws Exception
	 */
	@Test
	public void testC3P0_1() throws Exception {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc");
		dataSource.setUser("root");
		dataSource.setPassword("123456");

		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();
		dataSource.close();
	}

	/**
	 * 使用配置文件(xml配置文件要放在src下)
	 * 
	 * @throws Exception
	 */
	@Test
	public void testC3P0_2() throws Exception {
		DataSource dataSource = new ComboPooledDataSource("testC3P0");
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		System.out.println(((ComboPooledDataSource) dataSource).getMaxStatementsPerConnection());
		connection.close();
	}

	/**
	 * 使用配置文件(properties配置文件要放在src下)
	 * 
	 * @throws Exception
	 */
	@Test
	public void testC3P0_3() throws Exception {
		DataSource dataSource = new ComboPooledDataSource();
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		System.out.println(((ComboPooledDataSource) dataSource).getMaxStatementsPerConnection());
		connection.close();
	}
}
